﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AerolionMVC.Models;

namespace AerolionMVC.Controllers
{
    //[Authorize()]
    public class FlightController : Controller
    {
        IFlightRepository repo;

        public FlightController()
        {
            repo = new FlightRepository();
        }

        //
        // GET: /Flight/
        [Authorize()]
        public ActionResult Index()
        {
            IEnumerable<Flight> flights = repo.FindAllFlights();
            return View(flights);
        }

        //
        // GET: /Flight/Details/5
        public ActionResult Details(string id)
        {
            Flight f = repo.GetFlight(id);
            return View("DetailsScaffold",f);
        }

        //
        // GET: /Flight/Create
        public ActionResult Create()
        {
            Flight f = new Flight();
            return View("CreateScaffold",f);
        } 

        //
        // POST: /Flight/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                Flight f = new Flight();
                UpdateModel(f);
                repo.Add(f);
                repo.Save();

                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e);
                return View("CreateScaffold");
            }
        }

        //
        // GET: /Flight/Edit/5

        public ActionResult Edit(string id)
        {
            System.Diagnostics.Debug.WriteLine("xxxxxxxxxxxxx:" + id);

            Flight f = repo.GetFlight(id);
            return View("EditScaffold",f);
        }

        //
        // POST: /Flight/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, FormCollection collection)
        {
            try
            {
                Flight f = repo.GetFlight(id);
                System.Diagnostics.Debug.WriteLine("hhhhhhhhhhh:" + id);
                UpdateModel(f);
                repo.Save();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View("EditScaffold");
            }
        }
    }
}
